Public Class frmBaoCaoTongGiaTriNhapXuatTheoDonVi

    Dim DonVi_ID As String = ""

    Private Sub frmBaoCaoTongGiaTriNhapXuatTheoDonVi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbbNhapXuat.Focus()
        dtpTuNgay.Text = "01/01/" + CStr(Date.Now.Year)
        dtpDenNgay.Text = Date.Now.Date.ToString
    End Sub

#Region "txtDonVi"
    Private Sub txtDonVi_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDonVi.DoubleClick
        ShowFrmChonDonVi()
    End Sub

    Private Sub txtDonVi_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDonVi.KeyDown
        If (e.KeyCode = Keys.F1) Then
            ShowFrmChonDonVi()
        End If
    End Sub

    Public Sub ShowFrmChonDonVi()
        Dim frm As New frmDMDoiTuong
        frm.bCallForm = True
        frm.eDoiTuong = enumDoiTuong.DONVI
        frm.ShowDialog()
        If frm.selectedDT IsNot Nothing Then
            DonVi_ID = frm.selectedDT.KH
            txtDonVi.Text = frm.selectedDT.Ten
        Else
            DonVi_ID = ""
            txtDonVi.Text = ""
        End If
    End Sub
#End Region

    Private Sub btnBaoCao_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        'chon DVGN co ma cha giong txtdonvi.text = a (like 'a%' chon con)
        If (DonVi_ID <> "") Then
            Dim strsql As String
            strsql = "select * from viewDPNX where DVGN like '" & DonVi_ID & "%'"
            Dim tieude, thoigian As String
            thoigian = "( Từ ngày " + dtpTuNgay.Text + " đến ngày " + dtpDenNgay.Text + " )"

            If (cbbNhapXuat.SelectedIndex = 0) Then
                strsql &= " and NX = 'N'"
                tieude = "TỔNG GIÁ TRỊ NHẬP LỆNH TỪ "
            Else
                strsql &= " and NX = 'X'"
                tieude = "TỔNG GIÁ TRỊ XUẤT LỆNH CẤP CHO "
            End If
            tieude &= txtDonVi.Text.ToUpper
            Dim tungay, denngay As String
            tungay = CStr(dtpTuNgay.Value.Month) + "/" + CStr(dtpTuNgay.Value.Day) + "/" + CStr(dtpTuNgay.Value.Year)
            denngay = CStr(dtpDenNgay.Value.Month) + "/" + CStr(dtpDenNgay.Value.Day) + "/" + CStr(dtpDenNgay.Value.Year)
            strsql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
            Dim ds As DataSet
            ds = BaseDB.ExecSql_DataSet(strsql)
            Dim tong_tien As Double
            For Each row As DataRow In ds.Tables(0).Rows
                tong_tien += row("TGIATRI")
            Next
            Dim nnreport As New rptBaoCaoTongGiaTriNhapXuatTheoDonVi
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue(0, tieude)
            nnreport.SetParameterValue(1, thoigian)
            nnreport.SetParameterValue(2, DoiSoRaChu(tong_tien))
            'setparametter SLKH or SLTH de xuat ra bao cao
            'nnreport.SetParameterValue(3,)
            Dim frm As New frmReportViewer(nnreport)
            frm.ShowDialog()
        Else
            ShowMessages("Bạn chưa chọn ĐVGN")
        End If
    End Sub
End Class